<?php

namespace App\Http\Resources\OpenShop\InvestmentPromotion;

use App\Enums\OpenShop\InvestmentPromotion\SignOrderEnum;
use App\Enums\OpenShop\InvestmentPromotion\UserContractEnum;
use App\Http\Resources\BaseResource;
use App\Models\OpenShop\InvestmentPromotion\OpenShopContractTemplateModel;
use App\Service\OpenShop\Feishu\ConfigService;
use Illuminate\Http\Request;

class UserContractResource extends BaseResource
{
    public static $signOrderUserExtContract = null;

    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     * @return array
     */
    public function toArray($request): array
    {
        return [
            'orderNo'                          => $this->orderNo,
            'operateSubsidyAmount'             => $this->operateSubsidyAmount,
            'kindSubsidyAmount'                => $this->kindSubsidyAmount,
            'contractors'                      => $this->contractors,
            'contractorsText'                  =>
                ConfigService::getConfigText("openShop", "contractors", $this->contractors),
            'companyName'                      => $this->companyName,
            'businessLicenseNumber'            => $this->businessLicenseNumber,
            'address'                          => $this->address,
            'startTime'                        => formatDateYmd($this->startTime),
            'endTime'                          => formatDateYmd($this->endTime),
            'contractType'                     => $this->contractType,
            'contractTypeText'                 =>
                ConfigService::getConfigText("openShop", "contractType", $this->contractType),
            'policyTemplateId'                 => $this->policyTemplateId,
            'remark'                           => $this->remark ?: "",
            'year'                             => $this->year,
            'initialFee'                       => $this->initialFee,
            'technologyTransferFee'            => $this->technologyTransferFee,
            'deposit'                          => $this->deposit,
            'managementCost'                   => $this->managementCost,
            'managementCostEveryYear'          => $this->managementCostEveryYear,
            'decorationDeposit'                => $this->decorationDeposit,
            'refundDecorationDeposit'          => $this->refundDecorationDeposit,
            'guidanceFee'                      => $this->guidanceFee,
            'shopCount'                        => $this->shopCount,
            'extContract'                      =>
                $this->whenLoaded('userExtContractRel') ? UserExtContractResource::collection(
                    self::$signOrderUserExtContract ? $this->userExtContractRel->merge(self::$signOrderUserExtContract) : $this->userExtContractRel
                ) : null,
            'createAt'                         => $this->createdAt->timestamp > 0 ?
                date("Y-m-d H:i:s", $this->createdAt->timestamp) : '',
            'signCountDown'                    => $this->createdAt->timestamp + SignOrderEnum::SIGN_DEADLINE_SECONDS - time(),
            'template'                         => $this->resourceLoaded('templateRel') ? ContractTemplateListResource::collection([$this->templateRel])
                                                                                                                     ->getIterator()
                                                                                                                     ->current() : null,
            'errMsg'                           => $this->getErrMsg(),
            'signOrderCostPrice'               => $this->signOrderCostLists(),
            'version'                          => $this->version,
            'legalPerson'                      => $this->legalPerson,
            'businessLicenseImg'               => $this->getBusinessLicenseFullImg(),
            'businessLicenseContent'           => $this->businessLicenseContent,
            'contractStatus'                   => $this->contractStatus,
            'mode'                             => $this->mode,
            //加盟模式 1-传统模式 2分账模式 3 定金模式
            'modeText'                         => OpenShopContractTemplateModel::MODE_MAP_TEXT[$this->mode] ?? "",
            //加盟模式文本
            'inviterId'                        => $this->inviterId ?? "",
            //邀请人id
            'inviterName'                      => $this->inviterName ?? "",
            //邀请人名称
            'departmentName'                   => $this->departmentName ?? "",
            //部门名称
            'brokerageConfig'                  => $this->getMyBankShareConfig(),
            //分账模式信息-抽佣比例配置
            'depositAmountRatio'               => $this->depositAmountRatio,
            //分账模式信息
            'monthRealAmountCommissionCapping' => $this->monthRealAmountCommissionCapping,
            //分账模式信息
            'isCommissionCapping'              => $this->isCommissionCapping,
            //分账模式信息
            'amountCommissionCapping'          => $this->amountCommissionCapping,
            //分账模式信息
            'depositAmount'                    => $this->depositAmount,
            //定金模式信息

        ];
    }

    /**
     * 获取错误信息
     *
     * @return mixed|string
     */
    public function getErrMsg()
    {
        if ($exception = json_decode($this->exceptionMsg)) {
            $errMsg = $exception->errorTitle . "-" . $exception->message;
        } else {
            if ($exception) {
                $errMsg = $exception;
            } else {
                $errMsg = "";
            }
        }
        return $errMsg;
    }
}
